package com.penjin.android.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.nostra13.universalimageloader.utils.L;
import com.penjin.android.activity.MainActivity;
import com.penjin.android.activity.apply.MyApplyDetailActivity;
import com.penjin.android.domain.JPushData;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.service.UserService;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by lxl on 2016/3/10.
 */
public class JPushReceiver extends BroadcastReceiver {

    private static final String TAG = "JPush";

    private Context context ;

    @Override
    public void onReceive(Context context, Intent intent) {

        this.context = context ;

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));


            String TAG = parseTAG(bundle) ;
            if(TAG==null)
                TAG="解析的TAG为空";
            Log.d("JPush",TAG) ;

            JPushData jPushData = UserService.getInstance(context).getNewPush(TAG) ;

            bundle.putInt(TAG+"num", jPushData.getNum() + 1 );

            UserService.getInstance(context).setNewPush(bundle,TAG);



        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);


/*            String TAG = parseTAG(bundle) ;
            if(TAG==null)
                TAG="解析的TAG为空";
            Log.d("JPush",TAG) ;

            JPushData jPushData = UserService.getInstance(context).getNewPush(TAG) ;

            bundle.putInt(TAG+"num", jPushData.getNum() + 1 );

            UserService.getInstance(context).setNewPush(bundle,TAG);*/


        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");

            //打开自定义的Activity
            Intent i = new Intent(context,MainActivity.class);

            i.putExtra("JPush",true) ;

            //i.putExtra("key",JPushInterface.ACTION_NOTIFICATION_OPENED) ;
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK  );
            context.startActivity(i);

        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }


    }

    private String parseTAG(Bundle bundle){

        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

        Log.d("JPush",extras) ;

        int tag = 0 ;

        int num = 0 ;

        try {

            JSONObject jsonObject = new JSONObject(extras) ;

            tag = jsonObject.getInt("Tag") ;

            if( tag == 2 ){

                num = jsonObject.getInt("CompanyTag") ;

            }

        }catch (Exception e){

        }

        if( tag == 1 ){

            return JPushConstants.Zhang_Liang ;

        }else if( tag == 2 ){

            String detailsNum = null ;

            switch (num){

                case 1:
                    detailsNum = JPushConstants.NEWS ;
                    break;
                case 2:
                    detailsNum = JPushConstants.PAPER_CHECK ;
                    break;
                case 3:
                    detailsNum = JPushConstants.KAOQIN ;
                    break;
                case 4:
                    detailsNum = JPushConstants.KAOQIN_RESULT ;
                    break;
                default:

            }

            UserService.getInstance(context).setNewPush(bundle,detailsNum);

            num = UserService.getInstance(context).getSecretaryNum(detailsNum) + 1 ;

            UserService.getInstance(context).setSecretaryNum(detailsNum,num);

            return JPushConstants.Secretary ;

        }else {

            return null ;

        }

    }

    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it =  json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " +json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }



}
